import {
    isEmpty as isEmptyFunc,
    isNaN as isNaNFunc,
    isNumber as isNumberFunc,
    isUndefined as isUndefinedFunc,
    clone as cloneFunc,
    cloneDeep as cloneDeepFunc
} from 'lodash';

export const isNaN = isNaNFunc;
export const isNumber = isNumberFunc;
export const isUndefined = isUndefinedFunc;
export const isNotUndefined = args => !isUndefinedFunc(args);
export const clone = cloneFunc;
export const cloneDeep = cloneDeepFunc;

/**
 * 检查val是否为空
 * @param {*} val
 * @returns
 */
export const isEmpty = val => {
    if (isNumber(val)) {
        return false;
    }
    return isEmptyFunc(val);
};

/**
 * 检查val是否不为空，0被认为不为空，undefined/null/NaN/{}/[]都会被认为空
 * @param {*} val
 */
export const isNotEmpty = val => {
    if (isNaN(val)) {
        return false;
    }
    if (isNumber(val)) {
        return true;
    }
    return !isEmpty(val);
};

/**
 * 生成UUID字符串
 * @returns
 */
export const getUuid = () => {
    var s = [];
    var hexDigits = '0123456789abcdef';
    for (var i = 0; i < 6; i++) {
        s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
    }
    s[14] = '4';
    s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1);
    s[8] = s[13] = s[18] = s[23];
    var uuid = s.join('');
    return uuid;
};

/**
 * 获取URL中的参数
 * @param {*} name
 */
export const getUrlParam = name => {
    const url = document.location.toString();
    const arrObj = url.split('?');
    if (arrObj.length > 1) {
        const arrPara = arrObj[1].split('&');
        let arr;
        for (let i = 0; i < arrPara.length; i++) {
            arr = arrPara[i].split('=');
            if (arr != null && arr[0] === name) {
                return decodeURI(arr[1]);
            }
        }
    }
    return '';
};
/*
 * 将科学计数法的数字转为字符串
 * @param  num
 */
export const toNonExponential = num => {
    if (num == null) {
        return num;
    }
    if (typeof num == 'number') {
        var m = num.toExponential().match(/\d(?:\.(\d*))?e([+-]\d+)/);
        return num.toFixed(Math.max(0, (m[1] || '').length - m[2]));
    } else {
        return num;
    }
};
/**
 * 乘法 - js运算精度丢失问题
 * @param arg1  数1
 * @param arg2  数2
 */
export const floatMultiply = (arg1, arg2) => {
    arg1 = Number(arg1);
    arg2 = Number(arg2);
    if ((!arg1 && arg1 !== 0) || (!arg2 && arg2 !== 0)) {
        return null;
    }
    arg1 = toNonExponential(arg1);
    arg2 = toNonExponential(arg2);
    var n1, n2;
    var r1, r2; // 小数位数
    try {
        r1 = arg1.toString().split('.')[1].length;
    } catch (e) {
        r1 = 0;
    }
    try {
        r2 = arg2.toString().split('.')[1].length;
    } catch (e) {
        r2 = 0;
    }
    n1 = Number(arg1.toString().replace('.', ''));
    n2 = Number(arg2.toString().replace('.', ''));
    return (n1 * n2) / Math.pow(10, r1 + r2);
};
/**
 * 除法 - js运算精度丢失问题
 * @param arg1  数1
 * @param arg2  数2
 */
export const floatDivide = (arg1, arg2 = 100) => {
    arg1 = Number(arg1);
    arg2 = Number(arg2);
    if (!arg2) {
        return null;
    }
    if (!arg1 && arg1 !== 0) {
        return null;
    } else if (arg1 === 0) {
        return 0;
    }
    arg1 = toNonExponential(arg1);
    arg2 = toNonExponential(arg2);
    var n1, n2;
    var r1, r2; // 小数位数
    try {
        r1 = arg1.toString().split('.')[1].length;
    } catch (e) {
        r1 = 0;
    }
    try {
        r2 = arg2.toString().split('.')[1].length;
    } catch (e) {
        r2 = 0;
    }
    n1 = Number(arg1.toString().replace('.', ''));
    n2 = Number(arg2.toString().replace('.', ''));
    return floatMultiply(n1 / n2, Math.pow(10, r2 - r1));
};
/**
 * 将时间转为时间戳
 * @param timeStr  时间
 * @param stampNum  毫秒
 *
 */
export const getTimeStamp = (timeStr, stampNum) => {
    if (!timeStr) return Date.now();
    let t = timeStr;
    // eslint-disable-next-line no-useless-escape
    t = t > 0 ? t : t.toString().replace(/\-/g, '/');
    let times = new Date(t).getTime();
    if (typeof stampNum == 'number') {
        times += stampNum;
    }
    return times;
};

/**
 * 加载图片webp
 */
export const getOssImg = url => {
    if (!url) return url;
    return url + '?x-oss-process=image/format,webp';
};
